<?php
class CaseManager
{
	const PROCESS_MAKE_REPORT = 'MAKE_REPORT';
	const VAR_SITE_ID = 'site_id';
	const VAR_REPORT_ID = 'report_id';
	const VAR_URLS = 'urls';
	
	/**
	 * @param int $siteId
	 * @return Ezer_PropelCase
	 */
	public static function addCase($siteId)
	{
		// find process
		$process = Ezer_PropelStepPeer::retrieveProcessByName(self::PROCESS_MAKE_REPORT);
		if(!$process)
			throw new SeoException("Process [" . self::PROCESS_MAKE_REPORT . "] not found");
		
		// insert case
		$variables = array(
			self::VAR_SITE_ID => $siteId
		);
		$case = new Ezer_PropelCase();
		$case->setProcessId($process->getId());
		$case->setPriority(1);
		$case->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$case->setVariables($variables);
		$case->save();
		
		return $case;
	}
	
	public static function installProcess()
	{
		$varSiteId = new Ezer_Variable();
		$varSiteId->setName(self::VAR_SITE_ID);
		$varSiteId->setType('int');
		
		$varReportId = new Ezer_Variable();
		$varReportId->setName(self::VAR_REPORT_ID);
		$varReportId->setType('int');
		
		$varUrls = new Ezer_Variable();
		$varUrls->setName(self::VAR_URLS);
		$varUrls->setType('array');
		
		$process = new Ezer_PropelBusinessProcess();
		$process->setName(self::PROCESS_MAKE_REPORT);
		$process->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$process->addVariable($varSiteId);
		$process->addVariable($varReportId);
		$process->addVariable($varUrls);
		$process->save();
		
		$sequence = new Ezer_PropelSequence();
		$sequence->setName('main');
		$sequence->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$sequence->setContainer($process);
		$sequence->save();
		
		$activityCreateReport = new Ezer_PropelActivityStep();
		$activityCreateReport->setName('Create Report');
		$activityCreateReport->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$activityCreateReport->setClass('CreateReportActivity');
		$activityCreateReport->setArgs(array(self::VAR_SITE_ID));
		$activityCreateReport->setContainer($sequence);
		$activityCreateReport->save();
		
		$activityAnalyzeSite = new Ezer_PropelActivityStep();
		$activityAnalyzeSite->setName('Analyze Site');
		$activityAnalyzeSite->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$activityAnalyzeSite->setClass('AnalyzeSiteActivity');
		$activityAnalyzeSite->setArgs(array(self::VAR_SITE_ID, self::VAR_REPORT_ID));
		$activityAnalyzeSite->setContainer($sequence);
		$activityAnalyzeSite->save();
		
		$activityCrawl = new Ezer_PropelActivityStep();
		$activityCrawl->setName('Crawl');
		$activityCrawl->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$activityCrawl->setClass('CrawlActivity');
		$activityCrawl->setArgs(array(self::VAR_SITE_ID, self::VAR_REPORT_ID));
		$activityCrawl->setContainer($sequence);
		$activityCrawl->save();
		
		$items = new Ezer_AssignStepFromAttribute(self::VAR_URLS);
		
		$foreach = new Ezer_PropelForeach();
		$foreach->setName('Foreach Url');
		$foreach->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$foreach->setOrderType(Ezer_IntForeach::TYPE_PARALLEL);
		$foreach->setArg($items);
		$foreach->setContainer($sequence, 0);
		$foreach->save();

		$activityAnalyzeUrl = new Ezer_PropelActivityStep();
		$activityAnalyzeUrl->setName('Analyze Url');
		$activityAnalyzeUrl->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$activityAnalyzeUrl->setClass('AnalyzeUrlActivity');
		$activityAnalyzeUrl->setArgs(array(Ezer_IntForeach::ITERATED_ITEM, self::VAR_REPORT_ID));
		$activityAnalyzeUrl->setContainer($foreach);
		$activityAnalyzeUrl->save();
		
		$activitySendReport = new Ezer_PropelActivityStep();
		$activitySendReport->setName('Send Report');
		$activitySendReport->setStatus(Ezer_IntStep::STEP_STATUS_ACTIVE);
		$activitySendReport->setClass('SendReportActivity');
		$activitySendReport->setArgs(array(self::VAR_SITE_ID, self::VAR_REPORT_ID));
		$activitySendReport->setContainer($sequence);
		$activitySendReport->save();
	}
}